package com.zhuss.core.processor;

import java.util.Date;

import com.zhuss.exception.ExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.diss.DeleteDissBo;
import com.zhuss.bean.vo.core.diss.DetailDissVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.DissDao;
import com.zhuss.core.db.entity.Diss;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.JsonUtil;

/**
 * @description: 吐槽删除
 * @author: zhushanshuo
 * @create: Tue Feb 02 15:22:08 CST 2021
 **/
@Component("dissDelete")
public class DissDelete extends AbstractProcessor<Object, DeleteDissBo> implements Processor<Object, DeleteDissBo> {

    @Autowired
    private DissDao dissDao;

    @Autowired
	@Qualifier("dissRead")
	private DissRead dissRead;


    /**
     * 校验
     */
	@Override
    public void check(DeleteDissBo bo) throws DzcxException {
		if (bo.getDissId() == null || bo.getDissId() <=0l) {
			throw new DzcxException(ExceptionCode.意见反馈ID为空);
		}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteDissBo param) throws DzcxException {
    	Long dissId = param.getDissId();
    	String name = ((DetailDissVo) dissRead.detail(dissId)).getTitle();

    	Diss record = new Diss();
    	record.setDissId(dissId);
    	record.setDeleted(param.getDeleted());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	dissDao.delete(record);
    	param.setName(name);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteDissBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.意见反馈删除.key());
    	record.setTargetId(param.getDissId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
